<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>利用递归遍历数据</title>
</head>

<body>
  <script>
    var data = [{
      id: 1,
      name: '家电',
      goods: [{
        id: 11,
        gname: '冰箱',
        goods: [{
          id: 111,
          gname: '海尔'
        }, {
          id: 112,
          gname: '美的'
        },]
      }, {
        id: 12,
        gname: '洗衣机'
      }]
    }, {
      id: 2,
      name: '服饰'
    }];

    // 输入我们要的id好 返回数据对象
    function getId(json, id) {
      var o = {};
      json.forEach(function (val) {
        if (val.id == id) {
          o = val
        } else if (val.goods && val.goods.length > 0) {
          o = getId(val.goods, id);
        }
      })
      return o;
    }
    console.log(getId(data, 1));
    console.log(getId(data, 11));
    console.log(getId(data, 111));
  </script>
</body>


</html>